package parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class ScratchCourse {
	public static void main(String[] args) throws Exception{
		
        URL url = new URL("http://202.207.163.225/jwweb/ZNPK/Private/List_XNXQKC.aspx?xnxq=20161");
        HttpURLConnection con = null;
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        con.setRequestProperty("Referer", "http://202.207.163.225/jwweb/ZNPK/KBFB_LessonSel.aspx");
        con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        con.setRequestProperty("Host", "202.207.163.225");
        con.setRequestProperty("Cookie", "ASP.NET_SessionId=fre2jyvhyzt1jw3gny4tpniq");
        con.setDoInput(true);
        con.setDoOutput(true);
        
        OutputStream out = con.getOutputStream();
        InputStream in = con.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"gbk"));
        String line = null;
        StringBuffer buffer = new StringBuffer();
        while((line=reader.readLine())!=null){
        	buffer.append(line);
//            System.out.println(line);
        }
        reader.close();
        Document doc = Jsoup.parse(buffer.toString());
        Elements trs = doc.getElementsByTag("script");
        Pattern pattern = Pattern.compile("parent.theKC.innerHTML='(.*)';</script>");
        Matcher matcher = pattern.matcher(trs.toString());
        StringBuffer buffer1 = new StringBuffer();
        while(matcher.find()){
//        	System.out.println(matcher.group(1));
      	    buffer1.append(matcher.group(1));
        }
        File f = new File("course_scratch.txt") ;
        byte b[] = buffer1.toString().getBytes();
        try{
        	out = new FileOutputStream(f) ;
        	out.write(b);
        	out.close();
        } 
        catch (Exception e){
            e.printStackTrace();
        }
        con.connect();

    
	}

}
